package com.ssb.admin.modules.sys.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ssb.admin.common.utils.BeanUtils;
import com.ssb.admin.common.utils.R;
import com.ssb.admin.common.utils.WebUtils;
import com.ssb.admin.modules.sys.role.entity.SysRole;
import com.ssb.admin.modules.sys.role.entity.SysUserRole;
import com.ssb.admin.modules.sys.role.service.ISysRoleService;
import com.ssb.admin.modules.sys.role.service.ISysUserRoleService;
import com.ssb.admin.modules.sys.user.controller.dto.UserQuery;
import com.ssb.admin.modules.sys.user.dao.SysUserDao;
import com.ssb.admin.modules.sys.user.entity.SysUser;
import com.ssb.admin.modules.sys.user.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author duaichao
 * @since 2018-08-23
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements ISysUserService {
    private final ISysRoleService sysRoleService;
    private final ISysUserRoleService sysUserRoleService;
    @Autowired
    public SysUserServiceImpl(ISysRoleService sysRoleService, ISysUserRoleService sysUserRoleService) {
        this.sysRoleService = sysRoleService;
        this.sysUserRoleService = sysUserRoleService;
    }
    /**
     * source 拷贝到target
     * @param source 源
     * @param target 目标
     */
    private void updateBase(SysUser source, SysUser target){
        // 不拷贝密码
        source.setPassword(null);
        BeanUtils.copyProperties(source, target);
        baseMapper.updateById(target);
    }
    @Override
    public void updateByLoginName(SysUser sysUser) {
        Wrapper<SysUser> ew = new EntityWrapper<SysUser>().eq("login_name", sysUser.getLoginName());

        this.updateBase(sysUser,this.selectOne(ew));
    }

    @Override
    public R queryPage(UserQuery dto) {
        int limit = dto.getLimit();
        int offset = (dto.getPage() - 1) * limit;
        List<SysUser> sysUsers = this.baseMapper.selectByFilter(offset, limit, dto);
        for (SysUser sysUser : sysUsers) {
            if (sysUser.getRoleId() == null) {
                continue;
            }
            SysRole role = sysRoleService.selectOne(new EntityWrapper<SysRole>()
                    .eq("id", sysUser.getRoleId()));
            if (role != null) {
                sysUser.setRole(role);
            }
        }
        int total = this.baseMapper.selectByFilterCount(dto);
        return R.ok().list(total, sysUsers);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long save(SysUser sysUser) {
        if (sysUser.getId()==null) {
            sysUser.setSalt(UUID.randomUUID().toString());
            sysUser.setPassword(WebUtils.getDefaultPassword(sysUser.getCredentialsSalt(), sysUser.getPassword()));
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateTime(new Date());
            this.baseMapper.insert(sysUser);
            //保存用户角色
            this.saveUserRole(sysUser);
        }else{
            if (sysUser.getRoleId()!=null){
                //删除用户角色关系
                sysUserRoleService.delete(new EntityWrapper<SysUserRole>().eq("user_id", sysUser.getId()));
                //保存用户角色
                this.saveUserRole(sysUser);
            }
            sysUser.setUpdateTime(new Date());
            this.baseMapper.updateById(sysUser);
        }
        return sysUser.getId();
    }
    private void saveUserRole(SysUser sysUser){
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(sysUser.getRoleId());
        this.sysUserRoleService.insert(sysUserRole);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void del(SysUser sysUser) {
        this.baseMapper.deleteById(sysUser);
    }


}
